package prictice.lesson;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IRichBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;

import java.util.Map;

/**
 * @Package prictice.lesson
 * @Description:
 * @Author elwyn
 * @Date 2017/9/15 0:20
 * @Email elonyong@163.com
 */
public class MyBolt implements IRichBolt {
    OutputCollector outputCollector;

    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
        this.outputCollector = outputCollector;
    }

    int num = 0;
    String string;

    @Override
    public void execute(Tuple tuple) {
        try {
            string = tuple.getStringByField("log");
            if (string != null) {
                num++;
                System.out.println("lines :" + num + "   sessionId:" + string.split("\t")[1]);
            }
            outputCollector.ack(tuple);
        } catch (Exception e) {
            outputCollector.fail(tuple);
            e.printStackTrace();
        }

    }

    @Override
    public void cleanup() {

    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields(""));
    }

    @Override
    public Map<String, Object> getComponentConfiguration() {
        return null;
    }
}
